package org.komarichin.jdo.dao;

import org.komarichin.beans.IArticle;
import org.komarichin.beans.dao.IArticleDAO;
import org.komarichin.jdo.persistence.Article;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * &nbsp;
 * </p>
 * User: Michael Komarichin
 * Date: Jan 23, 2010
 * Time: 9:25:05 PM
 */
@Transactional(readOnly = true)
public class ArticleDAO extends JdoGenericLanguageDAOSupport<Article, Long> implements IArticleDAO<Article, Long> {

    private static final Logger log = LoggerFactory.getLogger(ArticleDAO.class);

    private static final long serialVersionUID = -8458158289176811620L;

    protected ArticleDAO(Class<Article> type) {
        super(type);
    }

    @Override
    @Transactional(readOnly = false)
    public Long saveOrUpdate(Article entity) {
        log.info("save or update entity: {}", entity);
        IArticle<Long> a = getJdoTemplate().getObjectById(getType(), entity.getId());
        if (a == null) {
            return save(entity).getId();
        } else {
            a.setUpdate();
            a.setContent(entity.getContent());
            a.setDescription(entity.getDescription());
            a.setKeyWords(entity.getKeyWords());
            a.setLanguage(entity.getLanguage());
            a.setName(entity.getName());
            a.setSource(entity.getSource());
            a.setTitle(entity.getTitle());
            a.setLink(entity.getLink());
            return a.getId();
        }
    }

    @Override
    public Article newInstance() {
        log.debug("create new instance article");
        return new Article();
    }
}
